package com.bricker.laboratorymanagement.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bricker.laboratorymanagement.business.entity.Experiment;
import com.bricker.laboratorymanagement.business.model.ExpSelectModel;
import com.bricker.laboratorymanagement.business.service.ExperimentService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/experiment")
public class ExperimentController {
    @Autowired
    private ExperimentService experimentService;

    /**
     * 分页多条件查询实验项目
     * @param expSelectModel
     * @return
     */
    @GetMapping
    public Page<Experiment> getExPerimentByPage(ExpSelectModel expSelectModel) {
        return experimentService.getExPerimentByPage(expSelectModel);
    }

    /**
     * 根据id集合批量删除实验项目
     * @param request
     * @param ids
     */
    @DeleteMapping
    public void deleteExperimentByIds(HttpServletRequest request, @RequestBody String ids) {
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        List<Long> idList= JSONObject.parseArray(ids,Long.class);
        experimentService.deleteExperimentByIds(uId,idList);
    }

    /**
     * 添加或修改实现项目
     * @param request
     *
     */
    @PutMapping
    public void saveOrUpdateExPeriment(HttpServletRequest request,@RequestBody String experiments){
        String token=request.getHeader("access_token");
        Long uId= Long.valueOf(JWT.decode(token).getAudience().get(0));
        List<Experiment> experimentList =JSONObject.parseArray(experiments,Experiment.class);
        experimentService.saveOrUpdateExPeriment(uId,experimentList);
    }

    @GetMapping("/getAllExPeriment")
    public List<Experiment> getAllExPeriment(){
        return  experimentService.list(Wrappers.<Experiment>lambdaQuery().eq(Experiment::getDelFlag,"0"));
    }

}
